package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Customer;
import com.example.demo.entity.User;
import com.example.demo.service.CustomerService;
import com.example.demo.dao.CustomerMapper;
import com.example.demo.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author Administrator
* @description 针对表【customer】的数据库操作Service实现
* @createDate 2023-02-28 15:39:21
*/
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>
    implements CustomerService{
@Autowired
CustomerMapper customerMapper;
    @Override
    public IPage pageCC(IPage<Customer> page, Wrapper wrapper) {
     return   customerMapper.pageCC(page,wrapper);
    }

    @Override
    public Customer login(Customer customer) {
        Customer one = getUserInfo(customer);
        if (one != null) {
            BeanUtil.copyProperties(one, customer, true);
            // 设置token
            String token = TokenUtils.genToken(one.getId().toString(), one.getPassword());
            one.setToken(token);
        }
        return one;
    }

    private Customer getUserInfo(Customer customer) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("no", customer.getNo());
        queryWrapper.eq("password", customer.getPassword());
        Customer one;
        one = getOne(queryWrapper); // 从数据库查询用户信息
        return one;
    }
}




